<?php


namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Lib\UploadLib;
use Dingo\Api\Http\Request;

class InitController extends BaseController
{
    public function init() {
        $user = $this->getUser();
        return $this->success(compact('user'));
    }

    public function safe_check(Request $request) {
        try {
            $app = $this->miniProgram();
            $this->validate($request, [
                'type' => 'required',
                'content' => 'required'
            ]);
            $input = $request->input();
            if ($input['type'] == 'file') {
                $cur_url = UploadLib::handle($request->file('file'));
                $url = public_path( 'upload/banner/' . $cur_url);
                $result = $app->content_security->checkImage($url);
                \Storage::disk('upload_banner')->delete($cur_url);
            }
            else
                $result = $app->content_security->checkText($input['content']);

            $predicts = $result['errcode'];
            $hit = false;
            if ($predicts != 0)
                $hit = true;

            return $this->success(['hit' => $hit]);
        }catch (\Exception $e) {
            \Log::error($e);
            return $this->failed($e->getMessage());
        }

    }
}

